package com.ca.ui.core.view.card;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.ImageUtils;

import java.util.ArrayList;
import java.util.List;

public class PaperCardImageView extends ConstraintLayout {

    private final List<ImageView> list = new ArrayList<>();
    private int count;

    public ImageView iv1;
    public ImageView iv2;
    public ImageView iv3;
    public TextView countView;

    public PaperCardImageView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public PaperCardImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PaperCardImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.paper_card_image_view_layout, this);
        iv1 = view.findViewById(R.id.iv_1);
        iv2 = view.findViewById(R.id.iv_2);
        iv3 = view.findViewById(R.id.iv_3);

        list.add(iv1);
        list.add(iv2);
        list.add(iv3);

        countView = view.findViewById(R.id.tv_imageCount);

        setOnClickListener(v -> {});
    }

    public void setCount(int count) {
        this.count = count;
        String text = this.count + "图";
        this.countView.setText(text);
        this.countView.setVisibility(this.count > 3 ? VISIBLE : GONE);
    }

    public int getCount() {
        return count;
    }

    public void setImageUrlArray(String[] imageUrlArray) {

        if (imageUrlArray == null) {
            imageUrlArray = new String[0];
        }

        setCount(imageUrlArray.length);
        for (int i = 0; i < imageUrlArray.length && i < 3; i++) {
            ImageUtils.loadImage(getContext(), imageUrlArray[i], get(i));
        }

        setVisibility(this.count == 0 ? GONE : VISIBLE);
    }

    public ImageView get(int index) {
        return list.get(index);
    }
}
